package lookie.swing.tree.samples;

import java.awt.BorderLayout;
import java.io.File;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.TreeNode;

import lookie.swing.tree.model.LazyTreeNode;
import lookie.swing.tree.model.LazyTreeNodeModel;
import lookie.swing.tree.model.util.LazyTreeNodeWillExpandListener;
import lookie.swing.tree.render.LabelProviderTreeCellRenderer;
import lookie.swing.tree.support.file.FileTreeNode;
import lookie.swing.tree.support.file.FileTreeNodeLabelProvider;
import lookie.swing.tree.support.file.FileTreeNodeProvider;


public class LazyTreeNodeSample {

	private LazyTreeNodeSample() {
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		FileSystemTreeNode root = new FileSystemTreeNode(null, null);
		FileSystemTreeNodeProvider provider = new FileSystemTreeNodeProvider();
		FileTreeNodeLabelProvider labelProvider = new FileTreeNodeLabelProvider();
		LazyTreeNodeModel model = new LazyTreeNodeModel(root, provider);
		LabelProviderTreeCellRenderer renderer = new LabelProviderTreeCellRenderer(
				labelProvider);

		JTree tree = new JTree(model);
		tree.setRootVisible(false);
		tree.setShowsRootHandles(true);

		tree.addTreeWillExpandListener(new LazyTreeNodeWillExpandListener(
				new FileSystemTreeNodeProvider()));
		tree.setCellRenderer(renderer);
		JScrollPane scrollPane = new JScrollPane(tree);
		frame.getContentPane().add(scrollPane, BorderLayout.CENTER);

		frame.pack();
		frame.setVisible(true);
	}

	private class FileSystemTreeNodeProvider extends FileTreeNodeProvider {

		@Override
		protected TreeNode createTreeNode(File file, TreeNode parent) {
			return new FileSystemTreeNode(file, parent);
		}

	}

	private class FileSystemTreeNode extends LazyTreeNode implements
			FileTreeNode {

		private File file;

		public FileSystemTreeNode(File file, TreeNode parent) {
			super(parent);
			this.file = file;
		}

		public File getFile() {
			return file;
		}

	}

	public static void main(String[] args) {
		new LazyTreeNodeSample();
	}

}
